package myPackage;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import net.jxta.credential.Credential;
import net.jxta.exception.PeerGroupException;
import net.jxta.impl.access.simpleACL.SimpleACLAccessService;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PeerGroupAdvertisement;
import net.jxta.protocol.PipeAdvertisement;
import myPackage.PipeUtil;
import net.Group.PrivatePeerGroup;
import myPackage.frmMain;


public class CreateNewGroupDialog extends JDialog {
		
	private JFrame parent;
	public PeerGroup newgroup;
	
	public CreateNewGroupDialog(JFrame parent, PeerGroup rootGroup){
		super(parent, "Create New Group", true);
		this.parent = parent;
		
		CreateNewGroupPane cngPane = new CreateNewGroupPane(CreateNewGroupDialog.this, rootGroup);
		
		this.setContentPane(cngPane);
		this.setBounds(200, 200, 400, 220);
		
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setResizable(false);

	}
}


class CreateNewGroupPane extends JPanel{
	private static final long MILLISECONDS_IN_A_DAY = 24 * 60 * 60 * 1000;
	private PeerGroup satellaPeerGroup = null;
	
	JPanel namePane = new JPanel();
	JLabel nameLabel = new JLabel("New Group Name: ");
	JTextField nameTxtFd = new JTextField(20);
	
	JPanel descPane = new JPanel();
	JLabel descLabel = new JLabel("          Description: ");
	JTextField descTxtFd = new JTextField(20);
	
	JCheckBox privateChkBox = new JCheckBox("Create Private Group");
	
	JPanel pwdPane = new JPanel();
	JLabel pwdLabel = new JLabel("Group Role: ");
	JComboBox roleList;
	//JPasswordField pwdPwdFd = new JPasswordField(20);
	
	//JPanel verifyPwdPane = new JPanel();
	//JLabel verifyPwdLabel = new JLabel("Verify Password: ");
	//JPasswordField verifyPwdPwdFd = new JPasswordField(20);
	
	JPanel btnsPane = new JPanel();
	JButton doCreateBtn = new JButton("OK");
	JButton cancelBtn = new JButton("Cancel");

	CreateNewGroupDialog parent;
	PeerGroup LotGroup = null;
	
	
	CreateNewGroupPane(final CreateNewGroupDialog parent, PeerGroup rootGroup){
		this.parent = parent;
		this.LotGroup = rootGroup;
		namePane.add(nameLabel);
		namePane.add(nameTxtFd);
		
		descPane.add(descLabel);
		descPane.add(descTxtFd);
		
		privateChkBox.setSelected(false);
		
		roleList = new JComboBox(RBACUtil.AllRoles());
		
		roleList.setEnabled(false);
		
		pwdPane.add(pwdLabel);
		pwdPane.add(roleList);
		
		//verifyPwdPane.add(verifyPwdLabel);
		//verifyPwdPane.add(verifyPwdPwdFd);
		
		btnsPane.add(doCreateBtn);
		btnsPane.add(cancelBtn);
		
		privateChkBox.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(privateChkBox.isSelected()){
					roleList.setEnabled(true);
					//verifyPwdPwdFd.setEnabled(true);
				}else{
					roleList.setEnabled(false);
					//verifyPwdPwdFd.setEnabled(false);
				}
			}
		});
		
		doCreateBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//TODO
				//String pwdStr = new String(pwdPwdFd.getPassword());
				//String vPwdStr = new String(verifyPwdPwdFd.getPassword());
				
//				if(privateChkBox.isSelected() && ! pwdStr.equals(vPwdStr)){
//					System.out.println("Two Passwords are not equal!" + pwdStr + ":" + vPwdStr);
//					JOptionPane.showMessageDialog((Component) null,
//							"Two Passwords are not equal!", "ERROR!",
//							JOptionPane.ERROR_MESSAGE);
//				}else {
					if(doCreatNewGroup())
						parent.dispose();
//				}
			}
		});
		
		cancelBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				parent.dispose();
			}
		});
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		//this.setAlignmentY(JComponent.LEFT_ALIGNMENT);
		this.add(namePane);
		this.add(descPane);
		this.add(privateChkBox);
		this.add(pwdPane);
		//this.add(verifyPwdPane);
		this.add(btnsPane);
	}
	
	public boolean doCreatNewGroup(){
		String rolename = (String) roleList.getSelectedItem();
		System.out.println("Name:	" + nameTxtFd.getText() + "");
		System.out.println("Description:	" + descTxtFd.getText() + "") ;
		System.out.println("CheckBox Stat:	" + privateChkBox.isSelected());
		System.out.println("Password:	" + rolename );
		//System.out.println("Verify Pwd:	" + new String(verifyPwdPwdFd.getPassword()));
		
		String name = (nameTxtFd.getText() + "").trim();
        //expiration days
		int expiration = 3;
        String description = (descTxtFd.getText() + "").trim();
        String password = null;
        if(privateChkBox.isSelected()){
        	password = (String) roleList.getSelectedItem();
        }
        
        password = password != null ? password.trim() : "";

        if (name.length() == 0) {
            name = null;
        }

        // only create groups which have a  non-empty name
        if (name != null) {

            // call createnewgroup in privatepeergroup to create new group;
            PrivatePeerGroup satellaRoot = new PrivatePeerGroup(LotGroup);
            
            satellaRoot.createPeerGroup(LotGroup, name);
            
            boolean join = satellaRoot.joinSecureGroup(satellaRoot.getPeerGroup(), LotGroup);
            //if the group was created and then join it
            if(join == true){            	
            	JOptionPane.showMessageDialog((Component) null,
    					"Successful joining to the group", "Succeed!",
    					JOptionPane.INFORMATION_MESSAGE);
            	//frmMain myapp = new frmMain();
            	Credential finalCredential = satellaRoot.getFinalCredential();
            	frmMain.reStartService(satellaPeerGroup,finalCredential);
    			return true;
            }
            else {JOptionPane.showMessageDialog((Component) null,
					"Can not join in the group, it's weird!", "Information",
					JOptionPane.INFORMATION_MESSAGE);
            }

        }
		return false;     
        }
}
